/********************************************************************************
* @file    app_main.h
* @author  jianqiang.xue
* @version V1.0.0
* @date    2021-04-24
* @brief   biz是业务函数的意思
********************************************************************************/

#ifndef __APP_MAIN_H
#define __APP_MAIN_H

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
//#include "app_led.h"
// #include "app_power.h"

#include "bsp_flash.h"
#include "ls_syscfg.h"

/* Public Define -------------------------------------------------------------*/
#define SYS_DEBUG_MODE                          1

#define BOOT_STATE_RUN_APP                      0
#define BOOT_STATE_IN_DFU                       1
#define BOOT_STATE_MOVE_OTA_IN_APP              2

#define SIGNAL_UART0_RX                         (1<<0)

#define SIGNAL_ADC0_COLLECTION                  (4<<1)
// #define SIGNAL_SYS_SHUT_DOWN                   (1<<3)
// #define SIGNAL_SYS_POWER_ON                    (1<<4)
// #define SIGNAL_SYS_DEEP_SLEEP                  (1<<5)

// #define SIGNAL_GOTO_LOW_POWER                  (1<<7)

#define SIGNAL_KV_WRITE_APP_IO_INFO            (10<<1)
#define SIGNAL_KV_WRITE_SYS_INFO               (11<<1)

#define SIGNAL_MAIN_MSGQUEUE_EVENT             (1<<29)
#define SIGNAL_SYS_GOTO_BOOT                   (1<<30)



#if LS_FLASH_INFO_TYPE
#include "biz_fds.h"
#define FLASH_WRITE_DATA    biz_fds_write_data
#define FLASH_GET_DATA      biz_fds_read_data
#else
#include "kv_sys.h"
#define FLASH_WRITE_DATA    kv_set_env
#define FLASH_GET_DATA      kv_get_env
#endif

/* Public Enum ----------------------------------------------------------------*/
typedef enum {
    SYS_STATE_SHUT_DOWN = 0,
    SYS_STATE_LOW_POWER,
    SYS_STATE_POWER_ON
} sys_state_t;

typedef enum { // 定义事件类型，在app_main中执行功能
    MAIN_EVENT_UART0_TX = 0,
    MAIN_EVENT_ATCMD_RX,
    // >0x80 无需申请空间
} main_event_t;
/* Public Struct --------------------------------------------------------------*/

typedef struct __attribute__((packed, aligned(1))) {
    uint8_t*  data;     // TX数据指针
    uint16_t  len;      // TX数据长度
    uint16_t  argv0;    // 参数0，举例：I2C/SPI时，作为器件地址
    uint16_t  argv1;    // 参数1  举例：I2C/SPI时，作为功能码  0-读 1-写
    uint8_t*  rx_data;  // RX数据指针
    uint16_t  rx_len;   // 接收数据长度
    main_event_t event; // 0--发送UART数据 1--UART接收到数据 2--I2C0操作  0x81--I2C0操作(常量)
} main_msgqueue_t;

typedef struct __attribute__((packed, aligned(1))) {
    sys_state_t run_state;
    uint8_t     low_power_support : 1; // 0--不支持 1--支持低功耗 注：低功耗状态，串口首次数据将用于唤醒，第二次数据才能生效
    uint8_t     write_in : 1; // flash是否写入 0--false 1--true
} system_info_t;

/* External Variables --------------------------------------------------------*/
extern system_info_t system_info;
extern boot_info_t g_boot_info;
/* Public Variables ----------------------------------------------------------*/
sys_state_t get_sys_state(void);
void set_sys_state(sys_state_t state);

void main_send_signal(int32_t signals);

void sys_switch(void);

void sys_time_reset(void);
void delay_ms(uint16_t ms);


void uart0_send(uint8_t* data, uint16_t len);
uint8_t main_enqueue(main_msgqueue_t *msg);
#endif
